package com.luo.chengrui.module.sa.controller.admin.datapermission;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.datapermission.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.datapermission.DataPermissionItemDO;
import com.luo.chengrui.module.sa.convert.datapermission.DataPermissionItemConvert;
import com.luo.chengrui.module.sa.service.datapermission.DataPermissionItemService;

@Tag(name = "管理后台 - 数据权限项，")
@RestController
@RequestMapping("/sa/data-permission-item")
@Validated
public class DataPermissionItemController {

    @Resource
    private DataPermissionItemService dataPermissionItemService;

    @PostMapping("/create")
    @Operation(summary = "创建数据权限项，")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item:create')")
    public CommonResult<String> createDataPermissionItem(@Valid @RequestBody DataPermissionItemCreateReqVO createReqVO) {
        return success(dataPermissionItemService.createDataPermissionItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据权限项，")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item:update')")
    public CommonResult<Boolean> updateDataPermissionItem(@Valid @RequestBody DataPermissionItemUpdateReqVO updateReqVO) {
        dataPermissionItemService.updateDataPermissionItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据权限项，")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item:delete')")
    public CommonResult<Boolean> deleteDataPermissionItem(@RequestParam("id") String id) {
        dataPermissionItemService.deleteDataPermissionItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据权限项，")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item:query')")
    public CommonResult<DataPermissionItemRespVO> getDataPermissionItem(@RequestParam("id") String id) {
        DataPermissionItemDO dataPermissionItem = dataPermissionItemService.getDataPermissionItem(id);
        return success(DataPermissionItemConvert.INSTANCE.convert(dataPermissionItem));
    }

    @GetMapping("/list")
    @Operation(summary = "获得数据权限项，每一项对应一个业务权限场景列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item:query')")
    public CommonResult<List<DataPermissionItemRespVO>> getDataPermissionItemList(@RequestParam("ids") Collection<String> ids) {
        List<DataPermissionItemDO> list = dataPermissionItemService.getDataPermissionItemList(ids);
        return success(DataPermissionItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据权限项，每一项对应一个业务权限场景分页")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item:query')")
    public CommonResult<PageResult<DataPermissionItemRespVO>> getDataPermissionItemPage(@Valid DataPermissionItemPageReqVO pageVO) {
        PageResult<DataPermissionItemDO> pageResult = dataPermissionItemService.getDataPermissionItemPage(pageVO);
        return success(DataPermissionItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据权限项， Excel")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item:export')")
    @OperateLog(type = EXPORT)
    public void exportDataPermissionItemExcel(@Valid DataPermissionItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DataPermissionItemDO> list = dataPermissionItemService.getDataPermissionItemList(exportReqVO);
        // 导出 Excel
        List<DataPermissionItemExcelVO> datas = DataPermissionItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "数据权限项，.xls", "数据", DataPermissionItemExcelVO.class, datas);
    }

}
